package com.ctl.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ctl.dto.*;

import com.ctl.entity.*;
import com.ctl.feign.UserClient;
import com.ctl.service.*;

import com.ctl.sign.SignPrefig;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;


import java.util.ArrayList;
import java.util.List;


@RestController
public class VideoController {
    @Autowired
    private VideoService service;
    @Autowired
    private RedisTemplate redisTemplate;


    @GetMapping("/recommends")
    public List<VideosDTO> getPage(@RequestParam(defaultValue = "1") Integer page,
                                   @RequestParam(defaultValue = "1") Integer per_page){
        return service.getPageVideoDTO(page,per_page);
    }

    @GetMapping("/user/videos") //获取用户上传的视频
    public List<FileDTO> getUserByVideo(String token){
        User user = (User) redisTemplate.opsForValue().get(token);
        QueryWrapper<Video> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uid",user.getId());
        List<Video> videos = service.list(queryWrapper);
        List<FileDTO> fileDTOS = new ArrayList<>();
        for (Video video : videos) {
            FileDTO fileDTO = new FileDTO();
            BeanUtils.copyProperties(video,fileDTO);
            fileDTOS.add(fileDTO);
        }
        return fileDTOS;
    }
    @GetMapping("/videos")  //获取视频列表
    public List<VideosDTO> getVideos(Integer page, Integer per_page,
                                 Integer category){

        return service.getByCategoryIdAll(page, per_page, category);
    }
    @Autowired
    private UserClient userClient;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private FavoriteService favoriteService;
    @GetMapping("/videos/{video_id}")   //视频详情
    public VideosAllDTO getVideos(String token, @PathVariable Integer video_id){
        VideosAllDTO videosAllDTO = new VideosAllDTO();
        //获取视频信息
        Video video = service.getById(video_id);
        //获取使用视频上传用户信息
        User userVideo = userClient.getUser(video.getUid());
        System.out.println("userVideo = " + userVideo);
        //同属性赋值
        UserFollowingDTO userFollowingDTO = new UserFollowingDTO();
        BeanUtils.copyProperties(userVideo,userFollowingDTO);
        //判断用户是否登录
        if (token==null){
            userFollowingDTO.setFollowed(false);
        }else {
            User user = (User) redisTemplate.opsForValue().get(token);
            userClient.getIsFollow(userVideo.getId(),user.getId());
            //处理是否收藏
            QueryWrapper<Favorite> favoriteQuery = new QueryWrapper<>();
            favoriteQuery.eq("uid",user.getId())
                    .eq("video_id",video_id);
            Favorite favorite = favoriteService.getOne(favoriteQuery);
            if (favorite==null){
                videosAllDTO.setFavorite(false);
            }else {
                videosAllDTO.setFavorite(true);
            }
        }
        //获取视频类别
        Category category = categoryService.getById(video.getCategoryId());
        video.setCategory(category.getName());
        //同属性赋值
        BeanUtils.copyProperties(userVideo,userFollowingDTO);
        videosAllDTO.setUploader(userFollowingDTO);
        BeanUtils.copyProperties(video,videosAllDTO);
        //处理播放次数
        Integer playedCount = (Integer) redisTemplate.opsForValue().get(SignPrefig.VIDEO_PLAYED_COUNT + video_id);
        videosAllDTO.setPlaysCount(playedCount);
        //todo:点赞数未处理，是否喜欢
        videosAllDTO.setLikesCount(0);
        videosAllDTO.setLiked(false);
        videosAllDTO.setDisliked(false);
        return videosAllDTO;
    }
    @Autowired
    private CommentService commentService;
    //视频评论列表
    @GetMapping("/videos/{video_id}/comments")
    public VideoCommentDTO getComments(@PathVariable Integer video_id,
                                             @RequestParam(defaultValue = "1")Integer page,
                                             @RequestParam(defaultValue = "1")Integer per_page){
        return commentService.videoCommentPage(video_id,page,per_page);
    }
}
